<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册新账号</title>
    <link rel="stylesheet" href="style.css">
    <link rel="icon" href="image/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 注册页面特定样式 */
        .form-container {
            background: rgba(255, 255, 255, 0.9);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.5);
            width: 380px;
            padding: 40px 30px;
            text-align: center;
            position: relative;
            overflow: hidden;
            z-index: 5;
        }
        
        .form-header {
            margin-bottom: 30px;
        }
        
        .form-header h1 {
            font-size: 2.5rem;
            color: #0f3460;
            background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #5563de);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 10px;
        }
        
        .form-header p {
            color: #666;
            font-size: 1rem;
        }
        
        .back-link {
            position: absolute;
            top: 20px;
            left: 20px;
            color: #666;
            text-decoration: none;
            font-size: 0.9rem;
            transition: color 0.3s;
        }
        
        .back-link:hover {
            color: #ff6b6b;
        }
        
        .error-message {
            color: #ff6b6b;
            font-size: 0.9rem;
            margin-top: -15px;
            margin-bottom: 15px;
            text-align: left;
            padding-left: 20px;
            display: none;
        }

        /* 登录页面特定样式补充 */
        .server-info {
            position: absolute;
            top: 20px;
            right: 20px;
            background: rgba(255, 255, 255, 0.15);
            backdrop-filter: blur(5px);
            padding: 12px 20px;
            border-radius: 10px;
            color: white;
            font-size: 0.9rem;
            z-index: 20;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }
        
        .server-info .ip {
            font-weight: bold;
            color: #4ecdc4;
        }
        
        /* Toast 样式 */
        .toast {
            position: fixed;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            padding: 12px 20px;
            border-radius: 8px;
            color: white;
            font-size: 0.9rem;
            z-index: 100;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            opacity: 0;
            animation: fadeIn 0.3s forwards;
        }
        
        .toast-success {
            background-color: #4ecdc4;
        }
        
        .toast-error {
            background-color: #ff6b6b;
        }
        
        .fade-out {
            animation: fadeOut 0.3s forwards;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translate(-50%, 20px); }
            to { opacity: 1; transform: translate(-50%, 0); }
        }
        
        @keyframes fadeOut {
            from { opacity: 1; transform: translate(-50%, 0); }
            to { opacity: 0; transform: translate(-50%, 20px); }
        }
    </style>
</head>
<body>
    <!-- 服务器信息显示 -->
    <div class="server-info">
        <div>服务器状态: <span class="online">●</span> 在线</div>
        <div>访问地址: <span class="ip" id="serverIp">加载中...</span></div>
    </div>
    
    <div class="form-container">
        <a href="index.html" class="back-link"><i class="fas fa-arrow-left"></i> 返回登录</a>
        
        <div class="form-header">
            <h1>注册新账号</h1>
            <p>创建您的云服务器账户</p>
        </div>
        
        <form id="registerForm">
            <div class="input-group" id="usernameGroup">
                <i class="fas fa-user"></i>
                <input type="text" id="username" placeholder="请输入账号,8位数字" required>
            </div>
            <div class="error-message" id="usernameError">账号格式不正确（8位数字）</div>
            
            <div class="input-group" id="passwordGroup">
                <i class="fas fa-lock"></i>
                <input type="password" id="password" placeholder="请输入密码,6-20位数字字母组合" required>
            </div>
            <div class="error-message" id="passwordError">密码长度需在6-20位之间，包含字母和数字</div>
            
            <div class="input-group" id="confirmPasswordGroup">
                <i class="fas fa-lock"></i>
                <input type="password" id="confirmPassword" placeholder="请确认密码" required>
            </div>
            <div class="error-message" id="confirmPasswordError">两次密码输入不一致</div>
            
            <button type="submit" class="login-btn">立即注册</button>
        </form>
        
        <div class="additional-links">
            <a href="index.html">已有账号？返回登录</a>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const serverIp = document.getElementById('serverIp');
            const usernameGroup = document.getElementById('usernameGroup');
            const passwordGroup = document.getElementById('passwordGroup');
            const confirmPasswordGroup = document.getElementById('confirmPasswordGroup');
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');
            const confirmPasswordError = document.getElementById('confirmPasswordError');
            
            // 创建 Toast 函数
            const createToast = (message, type = 'success') => {
                const toast = document.createElement('div');
                toast.className = `toast toast-${type}`;
                toast.textContent = message;
                document.body.appendChild(toast);
                
                setTimeout(() => {
                    toast.classList.add('fade-out');
                    setTimeout(() => {
                        document.body.removeChild(toast);
                    }, 300);
                }, 3000);
            };
            
            // 显示服务器IP
            serverIp.textContent = window.location.host;
            
            // 处理表单提交
            document.getElementById('registerForm').addEventListener('submit', function(e) {
                e.preventDefault();
                
                // 清除之前的错误状态
                usernameGroup.classList.remove('error');
                passwordGroup.classList.remove('error');
                confirmPasswordGroup.classList.remove('error');
                usernameError.style.display = 'none';
                passwordError.style.display = 'none';
                confirmPasswordError.style.display = 'none';
                
                const username = document.getElementById('username').value;
                const password = document.getElementById('password').value;
                const confirmPassword = document.getElementById('confirmPassword').value;
                
                // 验证账号格式（8位数字）
                const usernameRegex = /^\d{8}$/;
                if (!usernameRegex.test(username)) {
                    usernameGroup.classList.add('error');
                    usernameError.style.display = 'block';
                    return;
                }
                
                // 验证密码格式（6-20位，包含字母和数字）
                const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{6,20}$/;
                if (!passwordRegex.test(password)) {
                    passwordGroup.classList.add('error');
                    passwordError.style.display = 'block';
                    return;
                }
                
                // 验证两次密码是否一致
                if (password !== confirmPassword) {
                    confirmPasswordGroup.classList.add('error');
                    confirmPasswordError.style.display = 'block';
                    return;
                }
                
                // 显示加载效果
                const submitBtn = this.querySelector('button');
                submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 注册中...';
                submitBtn.disabled = true;
                
                // 发送请求到服务器
                fetch('http://49.235.120.243:8083/api/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                })
                .then(response => response.json())
                .then(data => {
                    // 重置按钮状态
                    submitBtn.innerHTML = '立即注册';
                    submitBtn.disabled = false;
                    
                    if (data.success) {
                        // 显示成功 Toast
                        createToast(data.message || '注册成功');
                        
                        // 1秒后跳转
                        setTimeout(() => {
                            window.location.href = 'index.html';
                        }, 1000);
                    } else {
                        // 显示错误 Toast
                        createToast(data.message || '注册失败', 'error');
                    }
                })
                .catch(error => {
                    // 显示网络错误 Toast
                    createToast('请求失败，请检查网络连接', 'error');
                    console.error('请求错误:', error);
                });
            });
            
            // 输入框交互效果
            const inputs = document.querySelectorAll('input');
            inputs.forEach(input => {
                input.addEventListener('focus', function() {
                    this.parentElement.classList.add('active');
                    if (this.id === 'username') {
                        usernameGroup.classList.remove('error');
                        usernameError.style.display = 'none';
                    } else if (this.id === 'password') {
                        passwordGroup.classList.remove('error');
                        passwordError.style.display = 'none';
                    } else if (this.id === 'confirmPassword') {
                        confirmPasswordGroup.classList.remove('error');
                        confirmPasswordError.style.display = 'none';
                    }
                    if (!this.value && this.placeholder) {
                        this.placeholder = '';
                    }
                });
                
                input.addEventListener('blur', function() {
                    if (!this.value && this.id === 'username') {
                        this.placeholder = '请输入账号';
                    } else if (!this.value && this.id === 'password') {
                        this.placeholder = '请输入密码';
                    } else if (!this.value && this.id === 'confirmPassword') {
                        this.placeholder = '请确认密码';
                    }
                });
            });
        });
    </script>
</body>
</html>